#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct ListNode {
	 int val;
	 ListNode *next;
	 ListNode(int x) : val(x), next(NULL) {}
 };

class Solution {
public:
	ListNode *removeNthFromEnd(ListNode *head, int n) {
		int i=1;
		ListNode* j=NULL;
		ListNode* ptr=head;
		while(ptr->next!=NULL){
			if(i>=n){
				if(j==NULL)
					j=head;
				else
					j=j->next;
			}
			i++;
			ptr=ptr->next;
		}
		if(j==NULL)
			head=head->next;
		else
			j->next=j->next->next;
		return head;
	}
};

void printList(ListNode* head){
	while(head!=NULL){
		cout<<head->val<<",";
		head=head->next;
	}
	cout<<endl;
}

int main(){
	Solution s;
	ListNode n1(1);
	ListNode n2(2);n1.next=&n2;
	ListNode n3(3);n2.next=&n3;
	ListNode n4(4);n3.next=&n4;
	ListNode n5(5);n4.next=&n5;

	int index;
	ListNode* head=&n1;
	while(true){
		cin>>index;
		head=s.removeNthFromEnd(head,index);
		printList(head);
	}
	printList(&n1);
}